//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 18
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using ProxyHelpers.EWS;

namespace CauseSchemaViolationThroughProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            CauseSchemaViolationThroughProxy(binding);
        }

        /// <summary>
        /// Causes a SoapException to be thrown due to schema violation (Listing 18-15)
        /// </summary>
        /// <param name="binding">Binding to use</param>
        /// 
        public static void CauseSchemaViolationThroughProxy(
                                 ExchangeServiceBinding binding)
        {
            PathToExtendedFieldType badProp = new PathToExtendedFieldType();

            // PropertyTagType is limited in the schema to 4 digits.  Use 5 
            // instead...
            //
            badProp.PropertyTag = "0x12345";
            badProp.PropertyType = MapiPropertyTypeType.String;

            // Issue a FindItem request and ask for our "bad" extended property
            //
            FindItemType findItem = new FindItemType();

            // Use our partial class extension overloads to reduce typing...
            //
            findItem.ItemShape = new ItemResponseShapeType(
                                   DefaultShapeNamesType.IdOnly,
                                   badProp);
            findItem.ParentFolderIds = new BaseFolderIdType[] { 
                         new DistinguishedFolderIdType(
                               DistinguishedFolderIdNameType.inbox) };

            // Our SoapException will be thrown when we call this line...
            //
            FindItemResponseType response = binding.FindItem(findItem);
        }

    }
}
